//
//  HistoryDetailViewController.swift
//  TinyShopKeeper
//
//  Created by rimi on 16/6/8.
//  Copyright © 2016年 yuying. All rights reserved.
//

import UIKit

class HistoryDetailViewController: UIViewController {

   
    
    @IBOutlet weak var button1: UIButton!
    
    @IBOutlet weak var button2: UIButton!

    //   比例scale
    //   人均 averageSpend
    //    receivable应收
    //   实收资本capital
    //    differential差额

 
    //堂食dinein_scale
    
    @IBOutlet weak var dinein_scale: UILabel!
    @IBOutlet weak var dinein_day: UILabel!
    @IBOutlet weak var dinein_week: UILabel!
    @IBOutlet weak var dinein_numberOfdesk: UILabel!
    @IBOutlet weak var dinein_numberOfPerson: UILabel!
    @IBOutlet weak var dinein_averageSpend: UILabel!
    @IBOutlet weak var dinein_receivable: UILabel!
    @IBOutlet weak var dinein_capital: UILabel!
    @IBOutlet weak var dinein_differential: UILabel!
    
    
    //外卖外卖takeout_scale
    

    @IBOutlet weak var takeout_scale: UILabel!
    @IBOutlet weak var takeout_day: UILabel!
    @IBOutlet weak var takeout_week: UILabel!
    @IBOutlet weak var takeout_numberOfdesk: UILabel!
    @IBOutlet weak var takeout_numberOfPerson: UILabel!
    @IBOutlet weak var takeout_averageSpend: UILabel!
    @IBOutlet weak var takeout_receivable: UILabel!
    @IBOutlet weak var takeout_capital: UILabel!
    @IBOutlet weak var takeout_differential: UILabel!
    
    var detailSource:[[String:AnyObject]] = [[String:AnyObject]]()
    var time:String?
    var shop_id:String?
    var tangshi:[String:AnyObject] = [String:AnyObject]()
    var waimai:[String:AnyObject] = [String:AnyObject]()
    
    
    var paramDic:[String:String] = [String:String]()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.title = "店铺\(shop_id)"
       self.title = UserInfo.shopNameById(shop_id!)
        self.view.backgroundColor = UIColor.whiteColor()
        print(time,shop_id)
        UserInfo.allShopID()
        loadDataSource()
    
    }

    //网络请求
    func loadDataSource(){
        
        var paramDictionary:[String:String] = [String:String]()
        paramDictionary.updateValue(shop_id!, forKey: "shop_id")
        paramDictionary.updateValue(time!, forKey: "time")
        
        NetTool.requestX("加载中。。。。", urlString: urlAppendGetParams(CANTENN_TAKEOUT_LIST_URL), paramDictionary: ["body" : paramDictionary], successAction: { (responseObject) -> Void in
            
            MBProgressHUD.showSuccess("加载成功")//改变状态
            MBProgressHUD.hideHUD()
//            print(responseObject)
 
            self.tangshi = responseObject["body"]!!["tangshi"] as! [String:AnyObject]
            self.waimai = responseObject["body"]!!["waimai"] as! [String:AnyObject]
         
            self.userface()//处理数据
            
            }) { (error) -> Void in
        }
        
    }
    
    
    func  userface(){
        
        
        print(self.tangshi)
        print(self.waimai)
        
        let a = (tangshi["sum_order_amount"]?.floatValue)!
        let b = (waimai["sum_order_amount"]?.floatValue)!
        let total = a + b
        let A = String(a / total)
        let B = String(b / total)
        print(A,B)
        
        self.dinein_scale.text = A
        self.dinein_day.text = self.time
        self.dinein_week.text = dateBecomeWeek(self.time!)
        self.dinein_numberOfdesk.text = self.tangshi["sum_table"] as? String
        self.dinein_numberOfPerson.text = self.tangshi["sum_people"] as? String
        self.dinein_averageSpend.text = self.tangshi["avg_people"] as? String
        self.dinein_receivable.text = self.tangshi["sum_goods_amount"] as? String
        self.dinein_capital.text = self.tangshi["sum_order_amount"] as? String
        self.dinein_differential.text = self.tangshi["sum_chae"] as? String
        
        
        self.takeout_scale.text = B
        self.takeout_day.text = self.time
        self.takeout_week.text = dateBecomeWeek(self.time!)
        self.takeout_numberOfdesk.text = "\(self.waimai["sum_table"]!)"
        self.takeout_numberOfPerson.text = "\(self.waimai["sum_people"]!)"
        self.takeout_averageSpend.text = "\(self.waimai["avg_people"]!)"
        self.takeout_receivable.text = "\(self.waimai["sum_goods_amount"]!)"
        self.takeout_capital.text = "\(self.waimai["sum_order_amount"]!)"
        self.takeout_differential.text = "\(self.waimai["sum_chae"]!)"
        
        
    }
    
    //日期变星期
    func dateBecomeWeek(date:String) -> String{
        
        let arrWeek = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let theDate = formatter.dateFromString(date)
        let calendar = NSCalendar.init(calendarIdentifier: NSCalendarIdentifierChinese)
        let week = calendar?.component(NSCalendarUnit.Weekday, fromDate: theDate!)
        
        return arrWeek[week!-1]
        
        
    }
    
    
    @IBAction func button1(sender: UIButton) {
        print("~~~~01")
         self.paramDic.updateValue("0,2,5", forKey: "dinner")
       
        
    }
    
    
    
    @IBAction func button2(sender: UIButton) {
        print("~~~~~02")
        self.paramDic.updateValue("1,3,6", forKey: "dinner")
    }
    
    
    
    //MARK:--Navigation,故事版的点击cell然后show详情页，来传值时需要这个方法
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

       
        
        //跳到的界面
        let historyDetailVc = segue.destinationViewController as! MemberDetailViewController


        //╮(╯▽╰)╭赋值附不起
//        if ((sender?.button1) != nil){
//            paramDic.updateValue("0,2,5", forKey: "dinner")
//        }
//        if ((sender?.button2) != nil){
//            paramDic.updateValue("1,3,6", forKey: "dinner")
//        }
       
        //属性传值
        self.paramDic.updateValue(shop_id!, forKey: "shop_id")
        self.paramDic.updateValue(time!, forKey: "time")
        historyDetailVc.paramDic = paramDic
        historyDetailVc.url = urlAppendGetParams(ORDER_DETAIL_LIST_URL)
        historyDetailVc.memberOrHistoryIsOne = false
        
        print("******")
        
    }


    
}
